home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8257 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: very simple newbie question
  5. Date: 29 Feb 1996 20:30:30 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb29133030@qcd.lanl.gov>
  8. References: <robertk-2802960012350001@robertk.accessone.com>
  9.     <TANMOY.96Feb28090444@qcd.lanl.gov> <3135B987.1DDE@ix.netcom.com>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: Norman Bullen's message of Thu, 29 Feb 1996 06:34:47 -0800
  14.  
  15. In article <3135B987.1DDE@ix.netcom.com>
  16. Norman Bullen <nbullen@ix.netcom.com> writes:
  17.  
  18. <snip>
  19. NB: > r:               scanf("%s/n",s);
  20. <snip>
  21. NB: > First, /n is not white space, it is the character '/' followed by the
  22. NB: > character 'n'. When /n appears as a part of the format string for
  23. NB: > scanf, it tells scanf to read and discard the characters / and n if
  24. NB: > they follow. As the end of line is not a /, the behaviour is not
  25. NB: > consistent with your code. Check your code before wasting people's
  26. NB: > time with it.
  27. NB: > 
  28. NB: > Next, whitespace is not ignored in scanf. It means read and discard
  29. NB: > all following whitespace. So, scanf has to continue reading till it
  30. NB: > finds a non-whitespace character, which it leaves unread. This
  31. NB: > explains the behaviour that you see.
  32. NB: > 
  33. NB: > Cheers
  34. NB: > Tanmoy
  35. NB: > -- 
  36. NB: What he's trying to say (but doesn't seem to express very well) is that 
  37. NB: the representation of a "newline" character should have a backward slash 
  38. NB: (the top end pointed left) instead of a forward slash.
  39.  
  40. No, what I am trying to say is that /n is valid in scanf, and I
  41. explain what is done. I also pointed out that people should check
  42. their code before posting worldwide. I also explained what \n in its
  43. stead would have done.
  44.  
  45. NB: 
  46. NB: That's not the real problem, however. scanf is a really poorly conceived 
  47. NB: function, IMHO. It rules are too complicated and it does not do what most 
  48. NB: people want it to do. For your situation, as shown in the sample code 
  49. NB: above, you want to use gets instead of scanf.
  50.  
  51. If _you_ don't find something easy to understand, does not mean it is
  52. ill-conceived. If I don't understand, well it is :-). Joking aside,
  53. scanf is very powerful, and I often dispense off the phase of reading a
  54. line with fgets (if you are advising people to use gets, you haven't
  55. read the FAQ). I certainly prefer scanf("%*[^\n]"); scanf("%*c"); over
  56. the equivalent fgets.
  57.  
  58. scanf is much maligned, but is easy to understand, compact, can be
  59. used safely and easy to read. Why should one advise against it?
  60.  
  61. Cheers
  62. Tanmoy
  63. --
  64. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  65. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  66. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  67. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  68. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  69. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  70.